<?php
/**
 * Index Controller
 *
 * @author Highstrike (highstrike@gmail.com)
 * @package CargoClaims
 * @version 1.0.dev
 */
class App_Ctrl_Index extends MVC_Ctrl
{
	/**
	 * Models used by this controller
	 *
	 * @var array
	 */
	public $m = array();

	/**
	 * Makes initializations useful for any action
	 */
	public function __construct()
	{
		parent::__construct();

		// data storage objects that will be used by the models
		$database = new MVC_Database(MVC_Core::$settings['database']);
		$this-> m['job'] = new App_Model_Job($database, $this->_session);
		$this-> m['remote'] = new App_Model_Remote($this->_session);

		// send current language to tpl
		$langs = array();
		foreach(MVC_Core::$settings['languages'] as $lang) {
			$current = $lang[0] == $this->_session-> language;
			$langs[] = array('id' => $lang[1], 'name' => $lang[0], 'current' => $current);
			if($current)
				$this->lang = $lang[1];
		}
		$this->langs = $langs;

		// send translations to tpl
		MVC_NLS::load_Groups('layout, header, menu, page, side');
		$this->_ = MVC_NLS::get_Object();

		// load remote translations
		$remote = $this-> m['remote']->data($this->_session-> language);
		$this->catlang = array();
		$this->typelang = array();
		foreach($remote->xpath('//page[@key="Property_JobCategory"]/string') as $one)
			$this->catlang[(string)$one['key']] = trim($one-> tr);
		foreach($remote->xpath('//page[@key="Property_EmploymentType"]/string') as $one)
			$this->typelang[(string)$one['key']] = trim($one-> tr);

		// output categories
		$categories = array();
		foreach($this-> m['job']->get_Categories() as $one)
			$categories[] = array(
				'url' => '?module=category&id='. $one['sid'],
				'name' => $this->catlang[$one['value']],
				'jobs' => $one['jobs']
			);
		$this->categories = $categories;

		// get featured companies
		$companies = array();
		foreach($this-> m['job']->get_Featured_Companies() as $one)
			$companies[] = array(
				'url' => "http://cargojobs.nl/company/{$one['sid']}/". generate_named_id($one['name']),
				'file' => "http://www.cargojobs.nl/files/pictures/{$one['file']}"
			);
		$this->companies = $companies;
	}

	/**
	 * Main index function
	 */
	public function action_Index()
	{
		// get featured
		$featured = array();
		foreach($this-> m['job']->get_Featured_Jobs() as $one) {
			// get categories
			$categories = array();
			$catids = explode(',', $one['cids']);
			foreach(explode('{,}', $one['categories']) as $idx => $two)
				$categories[] = array(
					'url' => '?module=category&id='. $catids[$idx],
					'name' => $this->catlang[$two],
					'last' => count(explode('{,}', $one['categories'])) == $idx + 1
				);

			// get job types
			$jobtypes = array();
			foreach(explode('{,}', $one['jobtypes']) as $three)
				$jobtypes[] = array(
					'class' => substr(strtolower((string)$three), 0, 4),
					'name' => isset($this->typelang[$three]) ? $this->typelang[$three] : 'Unknown'
				);

			// build le array
			$featured[] = array(
				'url' => "http://cargojobs.nl/display-job/{$one['sid']}/". generate_named_id($one['title']) .".html",
				'title' => $one['title'],
				'company_url' => "http://cargojobs.nl/company/{$one['user_sid']}/". generate_named_id($one['CompanyName']),
				'company' => $one['CompanyName'],
				'date' => date('d-m-Y', strtotime($one['date'])),
				'categories' => $categories,
				'jobtypes' => $jobtypes
			);
		}
		$this->featured = $featured;

		// get latest
		$latest = array();
		foreach($this-> m['job']->get_Latest_Jobs() as $one) {
			// get categories
			$categories = array();
			$catids = explode(',', $one['cids']);
			foreach(explode('{,}', $one['categories']) as $idx => $two)
				$categories[] = array(
					'url' => '?module=category&id='. $catids[$idx],
					'name' => $this->catlang[$two],
					'last' => count(explode('{,}', $one['categories'])) == $idx + 1
				);

			// get job types
			$jobtypes = array();
			foreach(explode('{,}', $one['jobtypes']) as $three)
				$jobtypes[] = array(
					'class' => substr(strtolower((string)$three), 0, 4),
					'name' => isset($this->typelang[$three]) ? $this->typelang[$three] : 'Unknown'
				);

			// build le array
			$latest[] = array(
				'url' => "http://cargojobs.nl/display-job/{$one['sid']}/". generate_named_id($one['title']) .".html",
				'title' => $one['title'],
				'company_url' => "http://cargojobs.nl/company/{$one['user_sid']}/". generate_named_id($one['CompanyName']),
				'company' => $one['CompanyName'],
				'date' => date('d-m-Y', strtotime($one['date'])),
				'categories' => $categories,
				'jobtypes' => $jobtypes
			);
		}
		$this->latest = $latest;

		// which body template to use, and run the view
		$this-> site_section = "index.html";
		$this-> view("layout.html");
	}

	/**
	 * Switch the language
	 */
	public function action_Lang()
	{
		// change lang
		$get = new MVC_Request('get');
		foreach(MVC_Core::$settings['languages'] as $lang)
			if($lang[1] == $get->value)
				$this->_session-> language = $lang[0];

		// redirect HEIR
		$this->redirect($_SERVER['HTTP_REFERER']);
	}
}